package org.xm.sk.service.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.xm.sk.service.data.BaseService;
import org.xm.sk.vo.e.Datalevel;
import org.xm.sk.vo.tb.Role;
import org.xm.sk.vo.tb.User;

@Service
public class XmUserDetailsService implements UserDetailsService {

    @Autowired
    private BaseService<User> userService;
    @Autowired
    private MessageSource messages;

    @SuppressWarnings("unchecked")
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
        Date lastPasswordResetDate = null;
        Set<User> users = (Set<User>) userService.findByName(username);
        if (null != users && !users.isEmpty()) {
            for (User user : users) {
                if (Datalevel.NORMAL == user.getDatalevel()) {
                    String password = user.getPsw();
                    int userid = user.getId();
                    String nickname = user.getNickname();
                    String phone = user.getPhone();
                    int organid = -1;
                    String organname = null;
                    if (null != user.getOrgan()) {
                        organid = user.getOrgan().getId();
                        organname = user.getOrgan().getName();
                    }
                    lastPasswordResetDate = user.getLastPasswordResetDate();
                    for (Role role : user.getRoles()) {
                        if (Datalevel.NORMAL == role.getDatalevel()) {
                            auths.add(
                                    new SimpleGrantedAuthority(role.getName()));
                        }
                    }
                    int hairdresserid = 0;
                    if (null != user.getHairdressers()
                            && !user.getHairdressers().isEmpty()) {
                        hairdresserid = user.getHairdressers().iterator().next()
                                .getId();
                    }
                    int shopid = 0;
                    if (null != user.getShops() && !user.getShops().isEmpty()) {
                        shopid = user.getShops().iterator().next().getId();
                    }
                    return new XmUser(userid, hairdresserid, shopid, username,
                            nickname, phone, password, organid, organname,
                            auths, lastPasswordResetDate);
                }
            }
        }
        throw new BadCredentialsException(
                messages.getMessage("login.loginmsg.loginfail.invalid.username",
                        null, "The User no exist.", null));
    }

}
